package com.bridgeintelligent.tag.customer.controller;

import com.bridgeintelligent.tag.customer.dto.CustomersDto;
import com.bridgeintelligent.tag.customer.entity.CustomersPage;
import com.bridgeintelligent.tag.customer.model.IssueTaskModel;
import com.bridgeintelligent.tag.customer.model.IssueTaskQueryModel;
import com.bridgeintelligent.tag.customer.service.IIssueTaskService;
import com.bridgeintelligent.tag.customer.vo.IssueTaskVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * @description
 * @author: grp 2021-02-23 13:52
 **/
@Api(tags = "下发任务管理")
@RestController
@AllArgsConstructor
@Validated
@Slf4j
@RequestMapping("api/issueTask")
public class IssueTaskController {

    IIssueTaskService issueTaskService;

    @ApiOperation(value = "修改下发任务", notes = "下发任务管理")
    @PostMapping("put")
    public CustomersDto<Object> put(@RequestBody @Valid IssueTaskModel model) {
        if(StringUtils.isBlank(model.getChannelId())){
            return CustomersDto.error("下发任务id不能为空！");
        }
        int count = issueTaskService.countByNameAndNotId(model.getIssueKey(),model.getIssueName());
        if(count>0){
            return CustomersDto.error("下发任务名称不能重复！");
        }
        if(!NumberUtils.isDigits(model.getCcis())){
            return CustomersDto.error("ccis工单名称必须为数字！");
        }
        issueTaskService.put(model);
        return CustomersDto.success();
    }

    @ApiOperation(value = "下发任务列表", notes = "下发任务管理")
    @GetMapping("list")
    public CustomersDto<CustomersPage<IssueTaskVO>> list(@ModelAttribute @Valid IssueTaskQueryModel model) {
        model.setPageNumber(model.getPageNumber() == null ? 1 : model.getPageNumber());
        model.setPageSize(model.getPageSize() == null ? 10 : model.getPageSize());
        CustomersPage<IssueTaskVO> page = issueTaskService.list(model);
        return CustomersDto.success(page);
    }

    @ApiOperation(value = "下发任务删除", notes = "下发任务管理")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "下发任务ID", required = true)})
    @DeleteMapping("del")
    public CustomersDto<Object> del(@NotBlank String id) {
        issueTaskService.del(id);
        return CustomersDto.success();
    }

    @ApiOperation(value = "详情", notes = "下发任务管理")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "下发任务id", required = true)})
    @GetMapping("detail")
    public CustomersDto<IssueTaskVO> detail(@NotBlank String id) {
        return CustomersDto.success(issueTaskService.getDetail(id));
    }

    @ApiOperation(value = "修改状态", notes = "下发任务管理")
    @PostMapping("changState")
    public CustomersDto<Object> changState(@RequestBody IssueTaskModel model) {
        if(StringUtils.isBlank(model.getId())){
            return CustomersDto.error("id不能为空！");
        }
        if(StringUtils.isBlank(model.getState())){
            return CustomersDto.error("状态不能为空！");
        }
        issueTaskService.changState(model);
        return CustomersDto.success();
    }

    @ApiOperation(value = "手工下发定时触发接口", notes = "手工下发定时触发接口")
    @ApiImplicitParams({})
    @GetMapping("workByHand")
    public CustomersDto<IssueTaskVO> workByHand() {
        issueTaskService.workByHand();
        return CustomersDto.success();
    }
}
